package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.service.IStudentService;
import com.hpjpw.ido.service.ITeacherService;
import com.hpjpw.ido.service.IWorkerService;
import com.hpjpw.idoapp.domain.StudentImportVo;
import com.hpjpw.idoapp.domain.TeacherImportVo;
import com.hpjpw.idoapp.domain.WorkerRegisterVo;
import com.hpjpw.idoapp.service.IDoWorkerService;
import com.hpjpw.idoapp.service.UserImportService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: RuoYi-IDo
 * @BelongsPackage: com.hpjpw.idoapp.controller
 * @Author: hpj
 * @CreateTime: 2021-01-08 20:48
 * @Description: 后台学生、老师、企业员工增加与删除
 */
@RestController
@RequestMapping("/ido/app/back/user")
public class IDoUserAddDeleteController extends BaseController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private UserImportService userImportService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IWorkerService workerService;
    @Autowired
    private IDoWorkerService iDoWorkerService;
    @Autowired
    private TokenService tokenService;

    /**
     * 新增学生
     */
    @PreAuthorize("@ss.hasPermi('ido:student:add')")
    @Log(title = "IDO新增学生", businessType = BusinessType.INSERT)
    @PostMapping("student")
    @ApiOperation("新增学生")
    public AjaxResult addStudent(@RequestBody StudentImportVo student) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        List<StudentImportVo> list = new ArrayList<>();
        list.add(student);
        userImportService.importStudentUser(list, operName);
        return AjaxResult.success("添加成功，用户名：s" + student.getNumber()
                + "，密码：" + configService.selectConfigByKey("sys.user.initPassword"));
    }

    /**
     * 删除学生
     */
    @PreAuthorize("@ss.hasPermi('ido:student:remove')")
    @Log(title = "IDO删除学生", businessType = BusinessType.DELETE)
    @DeleteMapping("/student/{ids}")
    @ApiOperation("删除学生")
    public AjaxResult removeStudent(@PathVariable Long[] ids) {
        for (Long id : ids) {
            userService.deleteUserById(studentService.selectStudentById(id).getUserId());
        }
        return toAjax(studentService.deleteStudentByIds(ids));
    }

    /**
     * 新增老师
     */
    @PreAuthorize("@ss.hasPermi('ido:teacher:add')")
    @Log(title = "IDO新增老师", businessType = BusinessType.INSERT)
    @PostMapping("/teacher")
    @ApiOperation("新增老师")
    public AjaxResult addTeacher(@RequestBody TeacherImportVo teacher) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        List<TeacherImportVo> list = new ArrayList<>();
        list.add(teacher);
        userImportService.importTeacherUser(list, operName);
        return AjaxResult.success("添加成功，用户名：t" + teacher.getNumber()
                + "，密码：" + configService.selectConfigByKey("sys.user.initPassword"));
    }

    /**
     * 删除老师
     */
    @PreAuthorize("@ss.hasPermi('ido:teacher:remove')")
    @Log(title = "IDO删除老师", businessType = BusinessType.DELETE)
    @DeleteMapping("/teacher/{ids}")
    @ApiOperation("删除老师")
    public AjaxResult removeTeacher(@PathVariable Long[] ids) {
        for (Long id : ids) {
            userService.deleteUserById(teacherService.selectTeacherById(id).getUserId());
        }
        return toAjax(teacherService.deleteTeacherByIds(ids));
    }

    /**
     * 新增企业人员
     */
    @PreAuthorize("@ss.hasPermi('ido:worker:add')")
    @Log(title = "IDO新增企业人员", businessType = BusinessType.INSERT)
    @PostMapping("/worker")
    @ApiOperation("新增企业人员")
    public AjaxResult addWorker(@RequestBody WorkerRegisterVo workerRegisterVo) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        return iDoWorkerService.addWorker(workerRegisterVo, operName);
    }

    /**
     * 删除企业人员
     */
    @PreAuthorize("@ss.hasPermi('ido:worker:remove')")
    @Log(title = "IDO删除企业人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/worker/{ids}")
    @ApiOperation("删除企业人员")
    public AjaxResult removeWorker(@PathVariable Long[] ids) {
        for (Long id : ids) {
            userService.deleteUserById(workerService.selectWorkerById(id).getUserId());
        }
        return toAjax(workerService.deleteWorkerByIds(ids));
    }

}
